const path = require('path')
const db = require('../db/index')
//添加文章的处理函数
exports.addArticle = (req,res)=>{
    //打印req.body
    // console.log(req.body);
    // console.log("------------------------->")
    // console.log(req.file)
    // res.send("ok")
    //我们先要判读图片文件是否符合验证规则
    if(!req.file || req.file.filename === "cover_img") res.cc("文章封面是必选参数")
    //将所有信息放入一个对象中
    const articleInfo = {
        ...req.body,
        //文章封面存放服务器的路径
        cover_img: path.join("/uploads",req.file.filename),
        pub_date: new Date(),
        author_id:req.user.id
    }
    const sql = `insert into ev_articles set ?`
    db.query(sql,articleInfo,(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows !== 1) return("发布文章失败")
        res.cc("发布文章成功",0)
    })
}
